<?php

class JsController extends CController
{
	/*
	protected function beforeAction($action)
	{
		Yii::app()->clientScript->registerCoreScript('jquery');
		return true;
	}
	*/

	/**
	 * Displays the login page
	 */
	public function actionProduct()
	{

		echo 'var productRule='.Product::model()->getTypeJson().';';
		echo 'function productTypeChange(typeId,productId,prompt)
		{
			prompt=((typeof prompt)=="undefined")?true:prompt;
			var typeObj = $("#"+typeId);
			var productObj = $("#"+productId).get(0);
			productObj.options.length=0;
			products = productRule[""+typeObj.val()];
			if(!!prompt)
			{
				var item = new Option("----------","");
			    productObj.options[productObj.options.length]=item;	
			}
			
			for(var product in products)
			{
				var item = new Option(products[product],product)
			    productObj.options[productObj.options.length]=item;
			}
		}
		
		';
	}
	
	public function actionUsertypeProducttype()
	{
		echo 'var producttypeRule='.Producttype::model()->getUsertypeJson().';';
		echo 'function usertypeChange(usertypeCode,producttypeCode,prompt)
		{
			prompt=((typeof prompt)=="undefined")?true:prompt;
			var usertypeObj = $("#"+usertypeCode);
			var producttypeObj = $("#"+producttypeCode).get(0);
			producttypeObj.options.length=0;
			producttypes = producttypeRule[""+usertypeObj.val()];
			if(prompt)
			{
				var item = new Option("----------","")
			    producttypeObj.options[producttypeObj.options.length]=item;	
			}
			for(var producttype in producttypes)
			{
			    var item = new Option(producttypes[producttype],producttype)
			    producttypeObj.options[producttypeObj.options.length]=item;
			}
			$(producttypeObj).change();
		}
		';
	}
	
	public function actionProducttypeProduct()
	{
		echo 'var productRule='.Product::model()->getProducttypeJson().';';
		echo 'function productTypeChange(typeId,productId,prompt)
		{
			prompt=((typeof prompt)=="undefined")?true:prompt;
			var typeObj = $("#"+typeId);
			var productObj = $("#"+productId).get(0);
			productObj.options.length=0;
			products = productRule[""+typeObj.val()];
			if(prompt)
			{
				var item = new Option("----------","")
			    productObj.options[productObj.options.length]=item;
			}
			for(var product in products)
			{
				var item = new Option(products[product],product)
			    productObj.options[productObj.options.length]=item;
			}
		}
		function generalValidate(typeId,serialnumberRow,trade_number)
		{
			var typeObj = $("#"+typeId);
			var serialnumberRowObj = $("#"+serialnumberRow);
			if(!typeObj||!serialnumberRowObj) return;
			//deal serialnumber
			if(typeObj.val()==16||typeObj.val()==27)
			{
				serialnumberRowObj.show();
			}
			else
			{
				serialnumberRowObj.hide();
			}
			//deal trade number
			var tradeNumberObj = $("#"+trade_number);
			if(typeObj.val()==5||typeObj.val()==24)
			{
				tradeNumberObj.val(1);
				tradeNumberObj.attr("readonly",true);
			}
			else
			{
				tradeNumberObj.attr("readonly",false);
			}
		}
		function treatyValidate(typeId,trade_number)
		{
			var typeObj = $("#"+typeId);
			//deal trade number
			var tradeNumberObj = $("#"+trade_number);
			if(typeObj.val()==9||typeObj.val()==17)
			{
				tradeNumberObj.attr("readonly",false);
			}
			else
			{
				tradeNumberObj.val(1);
				tradeNumberObj.attr("readonly",true);
			}
		}
		';
	}
	/**
	 * Displays the login page
	 */
	public function actionDepart()
	{
		echo 'var departRule='.Depart::model()->getEparchyJson().';';
		echo 'function departEparchyChange(eparchy_code,depart_code,prompt)
		{
			var eparchyObj = $("#"+eparchy_code);
			var departObj = $("#"+depart_code).get(0);
			departObj.options.length=0;
			departs = departRule[""+eparchyObj.val()];
			if(prompt)
			{
				var item = new Option("----------","")
				departObj.options[departObj.options.length]=item;
			}
			for(var depart in departs)
			{
				for(var departItem in departs[depart]);
				{
					var item = new Option(departs[depart][departItem],departItem)
			    	departObj.options[departObj.options.length]=item;
				}
			}
		}
		';
	}
	
	public function actionCommon()
	{
		echo '
		function autoSum(subids,sumid,num)
		{
			var sum = 0;
			
			for(var subid in subids)
			{
				if(num)
				{
					sum+=$("#"+subids[subid]+"").val()*1*$("#"+num+"").val();
				}
				else
				{
					sum+=$("#"+subids[subid]+"").val()*1;
				}
				
			}
			
			$("#"+sumid+"").val(sum);
		}
		function autoMinus(sumid,subid,minusid,num)
		{
			var sum = 0;
			$("#"+minusid+"").val($("#"+sumid+"").val()-$("#"+subid+"").val());
		}
		';
	}
	
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

}